
import http from "../../utils/http";
import { deserialize, serializing } from "../../hooks/serializingString";
import { search } from "../../API/search";

// pages/search/search.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchRecords: [],
    page_sizea: 10,
    page: 1,
    historicalSearch: ""
  },

  clearSearchRecords() {
    wx.setStorageSync("searchRecords", [])
    this.setData({
      searchRecords: []
    })
  },
  passToSubcomponents(e: Event) {
    const { currentTarget } = e
    const { dataset } = currentTarget
    let porpData = Object.values(dataset)[0]
    console.log(porpData);
    this.search(
      {
        detail: porpData
      }
    )
  },
  // /api/search/
  search(e) {
    console.log(e);
    const { detail: q } = e
    const { page_sizea, page } = this.data
    this.searchRecordsMaintenance(q)
    search(page_sizea, page, q).then((res) => {
      console.log(res);
      const { search_data } = res
      const query = serializing(search_data)
      wx.navigateTo({
        url: "/pages/list/list?meta=search&data=" + query + "&page=" + page + "&q=" + q
      })
    })
  },

  searchRecordsMaintenance(q) {
    const { searchRecords } = this.data;
    searchRecords.unshift(q)
    const result = Array.from(new Set(searchRecords))
    result.slice(0, 10)
    wx.setStorageSync("searchRecords", result)
    this.setData({
      searchRecords: result
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const isSearchRecordsType = wx.getStorageSync("searchRecords") instanceof Array;
    if (isSearchRecordsType) {

      this.setData({
        searchRecords: wx.getStorageSync("searchRecords")
      })
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})